In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px

1. Read the file¶

In [32]:
#1. بارگذاری داده‌ها
dff = pd.read_excel('cleardata.xlsx')
In [33]:
dff = dff.rename(columns={'managhe_shahrdari': 'region'})
In [34]:
## ostan tehran 
dw = dff[dff.Ostan == "Tehran"]
df = dw[dw.Shahrestan== "Tehran"]
In [35]:
df
Out[35]:
region masahat price age eskelet date Ostan Shahrestan
1 14.0 70.63 35962.06 1 botoni 1395/01/01 Tehran Tehran
3 2.0 196.16 173327.90 20 felezi 1395/01/01 Tehran Tehran
6 1.0 87.00 34482.77 1 botoni 1395/01/01 Tehran Tehran
8 6.0 108.88 60617.19 39 felezi 1395/01/02 Tehran Tehran
9 6.0 100.00 66000.00 4 botoni 1395/01/02 Tehran Tehran
... ... ... ... ... ... ... ... ...
331836 7.0 33.39 32944.00 1 felezi 1395/12/30 Tehran Tehran
331841 1.0 250.00 144000.00 1 botoni 1395/12/30 Tehran Tehran
331843 5.0 120.00 45000.00 1 botoni 1395/12/30 Tehran Tehran
331844 10.0 84.54 33120.42 2 botoni and felezi 1395/12/30 Tehran Tehran
331855 4.0 65.00 32000.00 3 botoni 1395/12/30 Tehran Tehran

153433 rows × 8 columns

2.Exploring the data¶

In [36]:
df.describe()
Out[36]:
region masahat price age
count 153433.000000 1.534330e+05 1.534330e+05 153433.000000
mean 8.033096 8.507458e+02 5.040692e+04 8.171782
std 5.271359 2.260169e+05 2.524764e+05 9.630402
min 1.000000 1.000000e+00 4.000000e-02 0.000000
25% 4.000000 6.053000e+01 2.856636e+04 1.000000
50% 6.000000 7.810000e+01 3.884573e+04 5.000000
75% 12.000000 1.045000e+02 5.321434e+04 13.000000
max 22.000000 8.840498e+07 7.100000e+07 1309.000000
In [37]:
df.head()
Out[37]:
region masahat price age eskelet date Ostan Shahrestan
1 14.0 70.63 35962.06 1 botoni 1395/01/01 Tehran Tehran
3 2.0 196.16 173327.90 20 felezi 1395/01/01 Tehran Tehran
6 1.0 87.00 34482.77 1 botoni 1395/01/01 Tehran Tehran
8 6.0 108.88 60617.19 39 felezi 1395/01/02 Tehran Tehran
9 6.0 100.00 66000.00 4 botoni 1395/01/02 Tehran Tehran
In [38]:
# محاسبه کوانتایل‌های ۲۰ و ۸۰ درصد
q_20 = df['price'].quantile(0.2)
q_80 = df['price'].quantile(0.8)

# فیلترینگ بر اساس کوانتایل‌ها
df = df[(df['price'] >= q_20) & (df['price'] <= q_80)]
In [39]:
# price / 10,000
df['price'] = df['price']%10000
df.head(2)
/tmp/ipykernel_22977/346169701.py:2: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

Out[39]:
region masahat price age eskelet date Ostan Shahrestan
1 14.0 70.63 5962.06 1 botoni 1395/01/01 Tehran Tehran
6 1.0 87.00 4482.77 1 botoni 1395/01/01 Tehran Tehran
In [40]:
df['price'] = np.log(df['price'])
df.head(5)
/home/anjel/.local/lib/python3.11/site-packages/pandas/core/arraylike.py:399: RuntimeWarning:

divide by zero encountered in log

/tmp/ipykernel_22977/3109476270.py:1: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

Out[40]:
region masahat price age eskelet date Ostan Shahrestan
1 14.0 70.63 8.693171 1 botoni 1395/01/01 Tehran Tehran
6 1.0 87.00 8.407996 1 botoni 1395/01/01 Tehran Tehran
40 5.0 103.83 7.819897 3 botoni 1395/01/03 Tehran Tehran
56 11.0 48.00 8.865499 18 botoni 1395/01/04 Tehran Tehran
62 2.0 49.16 7.968503 20 felezi 1395/01/05 Tehran Tehran
In [41]:
df['price'].isin([-np.inf]).sum()
Out[41]:
1631
In [42]:
df = df[~df['price'].isin([-np.inf])]
In [43]:
#check -inf value is clear
df['price'].isin([-np.inf]).sum()
Out[43]:
0
In [44]:
#برررسی عدد ۰ و منفی در ستون ها 
count_zero_or_negative1 = (df['masahat'] <= 0).sum()
print(count_zero_or_negative1, 'عدد منفی در  مساحت')

count_zero_or_negative2 = (df['price'] <= 0).sum()
print(count_zero_or_negative2, ' : عدد منفی در  یک متر مربع')

count_zero_or_negative3 = (df['age'] < 0).sum()
print(count_zero_or_negative3, ' : عدد منفی در سن بنا')
0 عدد منفی در  مساحت
0  : عدد منفی در  یک متر مربع
0  : عدد منفی در سن بنا
In [45]:
# value <0 is drop
# حذف سطرهایی که مقدار یکی از ستون‌ها (masahat، gheymat_1_metr_moraba، age_bana) منفی یا صفر باشد
df = df[(df['masahat'] > 0) & (df['price'] > 0) & (df['age'] > 0)]
In [46]:
#برررسی عدد ۰ و منفی در ستون ها 
count_zero_or_negative1 = (df['masahat'] <= 0).sum()
print(count_zero_or_negative1, 'عدد منفی در  مساحت')

count_zero_or_negative2 = (df['price'] <= 0).sum()
print(count_zero_or_negative2, ' قیمت :')

count_zero_or_negative3 = (df['age'] < 0).sum()
print(count_zero_or_negative3, ' : عدد منفی در سن بنا')
0 عدد منفی در  مساحت
0  قیمت :
0  : عدد منفی در سن بنا
In [47]:
df.isnull().sum()
Out[47]:
region        0
masahat       0
price         0
age           0
eskelet       0
date          0
Ostan         0
Shahrestan    0
dtype: int64
In [48]:
df.fillna(0, inplace=True)
df.isnull().sum()
Out[48]:
region        0
masahat       0
price         0
age           0
eskelet       0
date          0
Ostan         0
Shahrestan    0
dtype: int64
In [49]:
df.count()
Out[49]:
region        78313
masahat       78313
price         78313
age           78313
eskelet       78313
date          78313
Ostan         78313
Shahrestan    78313
dtype: int64
In [50]:
df.head(2)
Out[50]:
region masahat price age eskelet date Ostan Shahrestan
1 14.0 70.63 8.693171 1 botoni 1395/01/01 Tehran Tehran
6 1.0 87.00 8.407996 1 botoni 1395/01/01 Tehran Tehran
In [51]:
df.shape
Out[51]:
(78313, 8)
In [52]:
px.histogram(df, x='price')
In [53]:
px.histogram(df, x='masahat')
In [54]:
px.histogram(df, x='age')
In [55]:
px.scatter(df, x='price', y='masahat', 
           labels={"price": "gheymat_yek_metr_moraba bray har saze",
                    "masahat": "masahat_saze"},
           width=350, height=250)
In [57]:
from itertools import cycle

labels = {
    'Shahrestan': 'شهرستان',
    'price': 'قیمت',
    'Year': 'سال'
}

# رسم نمودار خطی با استفاده از plotly.express
fig = px.line(df, x='date', y='price', color='Shahrestan', width=600, height=350, labels=labels)

# تنظیم استایل خط‌های نمودار
styles = cycle([None, 'dashdot', 'dash', 'dot'])
for ostan in df['Shahrestan'].unique():
    fig.update_traces(selector=dict(name=ostan), line=dict(dash=next(styles)))

fig.update_yaxes(title_text='قیمت (تومان)')
fig.update_xaxes(title_text='سال')

# نمایش نمودار
fig.show()
In [58]:
# تعریف برچسب‌ها برای نمودار
df_labels = {
    'price': 'قیمت فروش (تومان)',
    'date': 'تاریخ',
    'masahat': 'مساحت (متر مربع)'
}

# رسم نمودار خطی با استفاده از plotly.express
fig = px.line(df, x='date', y='price', title='قیمت فروش میانه', labels=df_labels, width=500, height=250)

# افزودن حاشیه‌ها به نمودار
fig.update_layout(margin=dict(t=30))

# نمایش نمودار
fig.show()
In [59]:
 #رسم نمودار scatter برای دو ویژگی SepalLength و SepalWidth
plt.figure(figsize=(8, 6))
plt.scatter(df['masahat'], df['price'], color='blue', alpha=0.7)
plt.title('masaht vs. price')
plt.xlabel('price')
plt.ylabel('masahat')
plt.grid(True)
plt.show()
No description has been provided for this image
In [65]:
# محاسبه مجموع قیمت‌ها بر حسب نوع ساختمان
sum_by_type = df.groupby('eskelet')['price'].sum()

# نمودار دایره‌ای (Pie chart)
plt.figure(figsize=(8, 6))
plt.pie(sum_by_type, labels=sum_by_type.index, autopct='%1.1f%%', startangle=140, colors=['skyblue', 'lightgreen'])
plt.title('eskelet vs price')
plt.axis('equal')  # برای اطمینان از اینکه دایره‌ای باشد
plt.show()
No description has been provided for this image
In [64]:
# محاسبه مجموع قیمت‌ها بر حسب نوع ساختمان
sum_by_type = df.groupby('region')['price'].sum()

# نمودار دایره‌ای (Pie chart)
plt.figure(figsize=(8, 6))
plt.pie(sum_by_type, labels=sum_by_type.index, autopct='%1.1f%%', startangle=140, colors=['skyblue', 'lightgreen'])
plt.title('region vs price')
plt.axis('equal')  # برای اطمینان از اینکه دایره‌ای باشد
plt.show()
No description has been provided for this image
In [ ]: